Skip to content

Conversation

VincentLanglet
Copy link
Contributor

Closes phpstan/phpstan#12080

I copied the strategy from TooWideMethodReturnTypehintRule

$isFirstDeclaration = $method->getPrototype()->getDeclaringClass() === $method->getDeclaringClass();
if (!$method->isPrivate()) {
if (!$method->getDeclaringClass()->isFinal() && !$method->isFinal()->yes()) {
if (!$this->checkProtectedAndPublicMethods) {
return [];
}
if ($isFirstDeclaration) {
return [];
}
}
}

But I didn't add the isFirstDeclaration check yet because it removes almost every reported errors... (and no early return was added before). Also, I already don't really understand the check in TooWideMethodReturnTypehintRule

Cause we're getting

class WithoutChild
{
     public function foo(): ?int // No error, but it "should" be reported with checkPublicAndProtected
	{
		return 42;
	}
}

class A
{
	public function foo(): int|string // No error cause it's the first declaration
	{
		return 42;
	}
}

class B extends A
{
	public function foo(): int|string // never returns string so it can be removed from the return type => but it will crash class C.
	{
		return 42;
	}
}

class C extends B
{
	public function foo(): int|string // never returns int so it can be removed from the return type
	{
		return '42';
	}
}

@ondrejmirtes ondrejmirtes merged commit f4fb852 into phpstan:2.1.x Sep 7, 2025
449 of 454 checks passed
@ondrejmirtes
Copy link
Member

Thank you!

Yeah, the $isFirstDeclaration is definitely weird.

@ondrejmirtes
Copy link
Member

Please contribute docs update to Config Reference.

@ondrejmirtes
Copy link
Member

I'm waiting on that docs update :) Thank you.

@VincentLanglet
Copy link
Contributor Author

I'm waiting on that docs update :) Thank you.

Yes, on my todo list ; I'm back from my 2-3 day vacations

@ondrejmirtes
Copy link
Member

No problem, no pressure 😊

@VincentLanglet
Copy link
Contributor Author

phpstan/phpstan#13480

@VincentLanglet
Copy link
Contributor Author

Yeah, the $isFirstDeclaration is definitely weird.

Should we try to remove it from TooWideMethodReturnTypehintRule ?

@VincentLanglet
Copy link
Contributor Author

Yeah, the $isFirstDeclaration is definitely weird.

Should we try to remove it from TooWideMethodReturnTypehintRule ?

Edit: I tried and seems like it's made to not report thing like

public function ipsum(): ?string {
		return null;
	}

for method which are made to be overrided

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

by-ref @param-out should not complain about unused types unless final

2 participants